package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwt.util.JwtUtils;
import com.jwt.util.RsaUtils;
import com.jwt.util.SendMessageUtil;
import com.jwt.util.VerifyCodeUtil;
import com.wfx.entity.Result;
import com.wfx.entity.UserDto;
import com.wfx.entity.WxbCustomer;
import com.wfx.entity.WxbMemeber;
import com.wfx.mapper.WxbMemeberMapper;
import com.wfx.service.IWxbMemeberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.security.PrivateKey;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-26
 */
@Service
public class WxbMemeberServiceImpl extends ServiceImpl<WxbMemeberMapper, WxbMemeber> implements IWxbMemeberService {

    @Autowired
    private WxbMemeberMapper wxbMemeberMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result insertMemeber(WxbMemeber wxbMemeber) {
        int insert = wxbMemeberMapper.insert(wxbMemeber);
        String key = wxbMemeber.getPhone();
        String redisCode = stringRedisTemplate.boundValueOps(key).get();
        wxbMemeber.setMemeberId(System.currentTimeMillis()+"");
        if (redisCode.equals(wxbMemeber.getCode())){
            if (insert==1){
                return new Result(true,"注册成功");
            }else {
                return new Result(false,"注册失败");
            }
        }else {
            return new Result(false,"验证码错误");
        }

    }

    @Override
    public Result checkName(String account) {
        WxbMemeber wxbMemeber = new WxbMemeber();
        wxbMemeber.setAccount(account);
        QueryWrapper<WxbMemeber> wrapper = new QueryWrapper<>();
        wrapper.eq("account",wxbMemeber.getAccount());
        WxbMemeber wxbMemeber1 = wxbMemeberMapper.selectOne(wrapper);
        if (wxbMemeber1==null){
            return new Result(true,"无此用户，可以注册");
        }else {
            return new Result(false,"重复用户名,请更换用户名");
        }
    }


    //发送验证码
    public Result sendCode(String phone){
        //根据手机号码获取用户信息
        QueryWrapper<WxbMemeber> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        WxbMemeber wxbMemeber = wxbMemeberMapper.selectOne(queryWrapper);
        if (wxbMemeber != null) {
            return new Result(false,"手机号码不存在");
        }else{
            //随机产生4位数的验证码
            String code = VerifyCodeUtil.generateVerifyCode(4, "abcdqwertyuiop8912345672wqwe");
            //发送短信
            boolean success = SendMessageUtil.sendMsg(phone, code);
            if(success){
                //发送成功,将code保存到redis中
                String key = phone;
                System.out.println(key);
                stringRedisTemplate.boundValueOps(key).set(code);
                stringRedisTemplate.expire(key,60, TimeUnit.MINUTES);
                System.out.println(stringRedisTemplate.boundValueOps(key).get());
                return  new Result(true,"发送成功",code);
            }else{
                return  new Result(false,"短信发送失败");

            }

        }

    }



    private String genJwtToken(Object user){
        try {
            //从类路径中获取私钥
            String privateKeyPath = ResourceUtils.getFile("classpath:ras_private").getPath();
            PrivateKey privateKey = RsaUtils.getPrivateKey(privateKeyPath);

            String jwtToken = JwtUtils.generateTokenExpireInMinutes(user, privateKey, 30);
            System.out.println(jwtToken);
            return jwtToken;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
